package com.glodon.paas.monitor.server.exposed.facade.impl;

import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.glodon.paas.monitor.server.biz.domain.MonitorAlarmRulesEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorItemEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorSendRulesEntity;
import com.glodon.paas.monitor.server.biz.service.MonitorAlarmRulesService;
import com.glodon.paas.monitor.server.biz.service.MonitorItemService;
import com.glodon.paas.monitor.server.biz.service.MonitorSendRulesService;
import com.glodon.paas.monitor.server.constants.MonitorStatusEnum;
import com.glodon.paas.monitor.server.dto.MonitorAlarmRulesDTO;
import com.glodon.paas.monitor.server.dto.MonitorItemDTO;
import com.glodon.paas.monitor.server.dto.MonitorResultMsg;
import com.glodon.paas.monitor.server.dto.MonitorSendRulesDTO;
import com.glodon.paas.monitor.server.exception.MonitorException;
import com.glodon.paas.monitor.server.exception.ThrowableUtil;
import com.glodon.paas.monitor.server.exposed.facade.MonitorItemAlarmSendFacadeService;
import com.glodon.paas.util.DateUtil;

@Service("monitorItemAlarmSendFacadeService")
public class MonitorItemAlarmSendFacadeServiceImpl implements MonitorItemAlarmSendFacadeService {

	private static final Logger			logger	= LoggerFactory.getLogger(MonitorAppUserFacadeServiceImpl.class);

	@Autowired
	private MonitorItemService			monitorItemService;

	@Autowired
	private MonitorAlarmRulesService	monitorAlarmRulesService;

	@Autowired
	private MonitorSendRulesService		monitorSendRulesService;

	public MonitorResultMsg<ArrayList<MonitorItemDTO>> queryMonitorItem(MonitorItemDTO entity) {

		MonitorResultMsg<ArrayList<MonitorItemDTO>> result = new MonitorResultMsg<ArrayList<MonitorItemDTO>>();

		if (null == entity) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("queryMonitorItem entity is null.");
			result.setResultObject(null);
			return result;
		}
		ArrayList<MonitorItemDTO> monitorItemList = monitorItemService.queryMonitorItem(entity);
		result.setSuccess(true);
		result.setResultObject(monitorItemList);
		return result;
	}

	public MonitorResultMsg<MonitorItemEntity> findMonitorItemById(Long id) {

		MonitorResultMsg<MonitorItemEntity> result = new MonitorResultMsg<MonitorItemEntity>();

		if (null == id) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("findMonitorItemById id is null.");
			result.setResultObject(null);
			return result;
		}
		MonitorItemEntity monitorItemEntity = monitorItemService.selectByPrimaryKey(id);
		result.setSuccess(true);
		result.setResultObject(monitorItemEntity);
		return result;
	}

	public MonitorResultMsg<MonitorItemEntity> changeMonitorItemStatus(Long itemId, String itemStatus) {
		MonitorResultMsg<MonitorItemEntity> result = new MonitorResultMsg<MonitorItemEntity>();
		if (null == itemId || StringUtils.isBlank(itemStatus)) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("changeMonitorItemStatus itemId or itemStatus is null.");
			result.setResultObject(null);
		}

		MonitorItemEntity monitorItemEntity = monitorItemService.selectByPrimaryKey(itemId);
		if (null != monitorItemEntity) {
			monitorItemEntity.setItemStatus(itemStatus);
			monitorItemEntity.setUpdateTime(new Date());
			if (itemStatus.equals(MonitorStatusEnum.OFF.getCode())) {
				Integer interval = DateUtil.minBetweenDate(new Date(), monitorItemEntity.getEffectiveTime());
				if (null != interval && interval.intValue() > 0) {
					monitorItemEntity.setEffectiveTime(new Date());
				}
			}
			monitorItemService.updateByPrimaryKeySelective(monitorItemEntity);
			result.setSuccess(true);
			result.setResultObject(monitorItemEntity);
		}
		return result;
	}

	public MonitorResultMsg<MonitorItemEntity> saveMonitorItem(MonitorItemEntity monitorItemEntity) {

		MonitorResultMsg<MonitorItemEntity> result = new MonitorResultMsg<MonitorItemEntity>();

		if (null == monitorItemEntity || null == monitorItemEntity.getAppId()) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("saveMonitorItem monitorItemEntity is null.");
			result.setResultObject(null);
			return result;
		}

		if (null == monitorItemEntity.getCreateTime()) {
			monitorItemEntity.setCreateTime(new Date());
		}

		try {
			monitorItemService.insertSelective(monitorItemEntity);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorItemService_insertSelective_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("saveMonitorItem Exception." + ex);
			result.setResultObject(null);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(monitorItemEntity);
		return result;
	}

	public MonitorResultMsg<MonitorItemEntity> updateMonitorItem(MonitorItemEntity monitorItemEntity) {

		MonitorResultMsg<MonitorItemEntity> result = new MonitorResultMsg<MonitorItemEntity>();

		if (null == monitorItemEntity || null == monitorItemEntity.getAppId()) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("updateMonitorItem monitorItemEntity is null.");
			result.setResultObject(null);
			return result;
		}

		if (null == monitorItemEntity.getUpdateTime()) {
			monitorItemEntity.setUpdateTime(new Date());
		}

		try {
			monitorItemService.updateByPrimaryKeySelective(monitorItemEntity);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorItemService_updateByPrimaryKeySelective_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("updateMonitorItem Exception." + ex);
			result.setResultObject(null);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(monitorItemEntity);
		return result;
	}

	@Transactional
	public MonitorResultMsg<Boolean> deleteMonitorItem(Long itemId) {

		MonitorResultMsg<Boolean> result = new MonitorResultMsg<Boolean>();

		if (null == itemId) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("deleteMonitorItem itemId is null.");
			result.setResultObject(false);
			return result;
		}

		try {
			monitorSendRulesService.deleteByItemId(itemId);
			monitorAlarmRulesService.deleteByItemId(itemId);
			monitorItemService.deleteByPrimaryKey(itemId);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorItemService_deleteByPrimaryKey_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("deleteMonitorItem Exception." + ex);
			result.setResultObject(false);
			return result;
		}

		result.setSuccess(true);
		result.setResultObject(true);
		return result;
	}

	public MonitorResultMsg<ArrayList<MonitorAlarmRulesEntity>> queryMonitorAlarm(
			MonitorAlarmRulesDTO monitorAlarmRulesDTO) {

		MonitorResultMsg<ArrayList<MonitorAlarmRulesEntity>> result = new MonitorResultMsg<ArrayList<MonitorAlarmRulesEntity>>();

		if (null == monitorAlarmRulesDTO) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("queryMonitorAlarm entity is null.");
			result.setResultObject(null);
			return result;
		}
		ArrayList<MonitorAlarmRulesEntity> monitorAlarmRulesList = monitorAlarmRulesService
				.queryMonitorAlarm(monitorAlarmRulesDTO);
		result.setSuccess(true);
		result.setResultObject(monitorAlarmRulesList);
		return result;
	}

	public MonitorResultMsg<MonitorAlarmRulesEntity> findMonitorAlarmRulesById(Long id) {

		MonitorResultMsg<MonitorAlarmRulesEntity> result = new MonitorResultMsg<MonitorAlarmRulesEntity>();

		if (null == id) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("findMonitorAlarmRulesById id is null.");
			result.setResultObject(null);
			return result;
		}
		MonitorAlarmRulesEntity monitorAlarmRulesEntity = monitorAlarmRulesService.selectByPrimaryKey(id);
		result.setSuccess(true);
		result.setResultObject(monitorAlarmRulesEntity);
		return result;
	}

	public MonitorResultMsg<MonitorAlarmRulesEntity> changeMonitorAlarmStatus(Long alarmId, String alarmStatus) {
		MonitorResultMsg<MonitorAlarmRulesEntity> result = new MonitorResultMsg<MonitorAlarmRulesEntity>();
		if (null == alarmId || StringUtils.isBlank(alarmStatus)) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("changeMonitorAlarmStatus appId is null.");
			result.setResultObject(null);
		}

		MonitorAlarmRulesEntity entity = monitorAlarmRulesService.selectByPrimaryKey(alarmId);
		if (null != entity) {
			entity.setUpdateTime(new Date());
			entity.setAlarmStatus(alarmStatus);
			monitorAlarmRulesService.updateByPrimaryKeySelective(entity);
			result.setSuccess(true);
			result.setResultObject(entity);
		}
		return result;
	}

	@Transactional
	public MonitorResultMsg<MonitorAlarmRulesEntity> saveMonitorAlarmRules(MonitorAlarmRulesEntity alarmRulesEntity) {

		MonitorResultMsg<MonitorAlarmRulesEntity> result = new MonitorResultMsg<MonitorAlarmRulesEntity>();

		if (null == alarmRulesEntity) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("saveMonitorAlarmRules entity is null.");
			result.setResultObject(null);
			return result;
		}
		if (null == alarmRulesEntity.getCreateTime()) {
			alarmRulesEntity.setCreateTime(new Date());
		}

		try {
			monitorAlarmRulesService.insertSelective(alarmRulesEntity);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorAlarmRulesService_insertSelective_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("saveMonitorAlarmRules Exception." + ex);
			result.setResultObject(null);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(alarmRulesEntity);
		return result;
	}

	public MonitorResultMsg<MonitorAlarmRulesEntity> updateMonitorAlarmRules(MonitorAlarmRulesEntity entity) {

		MonitorResultMsg<MonitorAlarmRulesEntity> result = new MonitorResultMsg<MonitorAlarmRulesEntity>();

		if (null == entity) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("updateMonitorAlarmRules entity is null.");
			result.setResultObject(null);
			return result;
		}
		if (null == entity.getUpdateTime()) {
			entity.setUpdateTime(new Date());
		}

		try {
			monitorAlarmRulesService.updateByPrimaryKeySelective(entity);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorAlarmRulesService_updateByPrimaryKeySelective_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("updateMonitorAlarmRules Exception." + ex);
			result.setResultObject(null);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(entity);
		return result;
	}

	@Transactional
	public MonitorResultMsg<Boolean> deleteMonitorAlarmRulesById(Long alarmId) {

		MonitorResultMsg<Boolean> result = new MonitorResultMsg<Boolean>();
		if (null == alarmId) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("deleteMonitorAlarmRulesById alarmId is null.");
			result.setResultObject(false);
			return result;
		}
		try {
			monitorSendRulesService.deleteByAlarmId(alarmId);
			monitorAlarmRulesService.deleteByPrimaryKey(alarmId);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorAlarmRulesService_deleteByPrimaryKey_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("deleteMonitorAlarmRulesById Exception." + ex);
			result.setResultObject(false);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(true);
		return result;
	}

	public MonitorResultMsg<ArrayList<MonitorSendRulesEntity>> queryMonitorSend(MonitorSendRulesDTO monitorSendRulesDTO) {

		MonitorResultMsg<ArrayList<MonitorSendRulesEntity>> result = new MonitorResultMsg<ArrayList<MonitorSendRulesEntity>>();

		if (null == monitorSendRulesDTO) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("queryMonitorSend monitorSendRulesDTO is null.");
			result.setResultObject(null);
			return result;
		}

		ArrayList<MonitorSendRulesEntity> monitorSendRulesList = monitorSendRulesService
				.queryMonitorSend(monitorSendRulesDTO);
		result.setSuccess(true);
		result.setResultObject(monitorSendRulesList);
		return result;
	}

	public MonitorResultMsg<MonitorSendRulesEntity> findMonitorSendRulesById(Long id) {

		MonitorResultMsg<MonitorSendRulesEntity> result = new MonitorResultMsg<MonitorSendRulesEntity>();

		if (null == id) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("findMonitorSendRulesById id is null.");
			result.setResultObject(null);
			return result;
		}
		MonitorSendRulesEntity monitorSendRulesEntity = monitorSendRulesService.selectByPrimaryKey(id);
		result.setSuccess(true);
		result.setResultObject(monitorSendRulesEntity);
		return result;
	}

	@Transactional
	public MonitorResultMsg<MonitorSendRulesEntity> saveMonitorSendRules(MonitorSendRulesEntity sendRulesEntity) {

		MonitorResultMsg<MonitorSendRulesEntity> result = new MonitorResultMsg<MonitorSendRulesEntity>();

		if (null == sendRulesEntity) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("saveMonitorItemSendRules entity is null.");
			result.setResultObject(null);
			return result;
		}

		if (null == sendRulesEntity.getCreateTime()) {
			sendRulesEntity.setCreateTime(new Date());
		}

		try {
			monitorSendRulesService.insertSelective(sendRulesEntity);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorItemAlarmRulesService_insertSelective_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("saveMonitorItemSendRules Exception." + ex);
			result.setResultObject(null);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(sendRulesEntity);
		return result;
	}

	public MonitorResultMsg<MonitorSendRulesEntity> updateMonitorSendRules(MonitorSendRulesEntity entity) {

		MonitorResultMsg<MonitorSendRulesEntity> result = new MonitorResultMsg<MonitorSendRulesEntity>();

		if (null == entity) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("updateMonitorSendRules entity is null.");
			result.setResultObject(null);
			return result;
		}
		if (null == entity.getUpdateTime()) {
			entity.setUpdateTime(new Date());
		}

		try {
			monitorSendRulesService.updateByPrimaryKeySelective(entity);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorItemAlarmRulesService_updateByPrimaryKeySelective_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("updateMonitorSendRules Exception." + ex);
			result.setResultObject(null);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(entity);
		return result;
	}

	@Transactional
	public MonitorResultMsg<Boolean> deleteMonitorSendRules(Long id) {

		MonitorResultMsg<Boolean> result = new MonitorResultMsg<Boolean>();

		if (null == id) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("deleteMonitorSendRules id is null.");
			result.setResultObject(false);
			return result;
		}

		try {
			monitorSendRulesService.deleteByPrimaryKey(id);
		} catch (MonitorException ex) {
			MonitorException mEx = ThrowableUtil.clearStackTrace(ex);
			logger.error("monitorItemAlarmRulesService_deleteByPrimaryKey_exception", mEx);
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("deleteMonitorSendRules Exception." + ex);
			result.setResultObject(false);
			return result;
		}
		result.setSuccess(true);
		result.setResultObject(true);
		return result;
	}

	public MonitorResultMsg<MonitorSendRulesEntity> changeMonitorSendStatus(Long sendId, String sendStatus) {
		MonitorResultMsg<MonitorSendRulesEntity> result = new MonitorResultMsg<MonitorSendRulesEntity>();
		if (null == sendId || StringUtils.isBlank(sendStatus)) {
			result.setSuccess(false);
			result.setResultCode(MonitorException.ErrorCode.PARAM_INVALID);
			result.setResultMsg("changeMonitorSendStatus sendId is null.");
			result.setResultObject(null);
		}

		MonitorSendRulesEntity entity = monitorSendRulesService.selectByPrimaryKey(sendId);
		if (null != entity) {
			entity.setUpdateTime(new Date());
			entity.setSendStatus(sendStatus);
			monitorSendRulesService.updateByPrimaryKeySelective(entity);
			result.setSuccess(true);
			result.setResultObject(entity);
		}
		return result;
	}

}
